package com.iotproject.eduorder.service.impl;

import com.iotproject.commonutils.ordervo.CourseWebVoOrder;
import com.iotproject.commonutils.ordervo.UcenterMemberOrder;
import com.iotproject.eduorder.client.EduClient;
import com.iotproject.eduorder.client.UcenterClient;
import com.iotproject.eduorder.entity.CourseOrder;
import com.iotproject.eduorder.mapper.OrderMapper;
import com.iotproject.eduorder.service.OrderService;
import com.iotproject.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, CourseOrder> implements OrderService {

    @Autowired
    EduClient eduClient;

    @Autowired
    UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String memberIdByJwtToken) {
        //根据课程ID查询课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        //根据用户ID查询用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberIdByJwtToken);

        //创建订单对象
        CourseOrder courseOrder = new CourseOrder();
        //使用工具类生成订单号
        courseOrder.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        //不能使用工具类注入，因为属性对不上
        courseOrder.setCourseId(courseId); //课程id
        courseOrder.setCourseTitle(courseInfoOrder.getTitle());
        courseOrder.setCourseCover(courseInfoOrder.getCover());
        courseOrder.setTeacherName(courseInfoOrder.getTeacherName());
        courseOrder.setTotalFee(courseInfoOrder.getPrice());
        courseOrder.setMemberId(memberIdByJwtToken);
        courseOrder.setMobile(userInfoOrder.getMobile());
        courseOrder.setNickname(userInfoOrder.getNickname());
        courseOrder.setStatus(0);  //订单状态（0：未支付 1：已支付）
        courseOrder.setPayType(1);  //支付类型 ，微信1
        baseMapper.insert(courseOrder);
        System.out.println("getOrderNo: " + courseOrder.getOrderNo());
        return courseOrder.getOrderNo();
    }
}
